package de.tum.team26.eistpoll.client.UI;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import de.tum.team26.eistpoll.client.EIST_POLL;

/**
 * Class to show the Poll
 */
public class Poll extends UIElement {

	//Controller
	EIST_POLL controller;
	
	// Container
	VerticalPanel pollPanel = new VerticalPanel();
	// Button
	Button btnSend = new Button("Send"); // Submitt Button for Poll
	// Labels
	Label lblOption = new Label("How statisfied are you with today's lecture?");
	Label lblLike = new Label("What did you like most?");
	Label lblDislike = new Label("What did you dislike or did not understand at all?");
	Label lblLikeRes = new Label("Result: What did you like most?");
	Label lblDislikeRes = new Label("Result: What did you dislike or did not understand at all?");
	Label lblHeadding;
	Label lblErrMsg;
	// Radio Buttons
	RadioButton rdbtnVerySatisfied = new RadioButton("optsatisfied","Very satisfied");
	RadioButton rdbtnUnsatisfied = new RadioButton("optsatisfied", "Unsatisfied");
	RadioButton rdbtnNeutral = new RadioButton("optsatisfied", "Neutral");
	RadioButton rdbtnSatisfied = new RadioButton("optsatisfied", "Satisfied");
	RadioButton rdbtnVeryUnsatisfied = new RadioButton("optsatisfied","Very unsatisfied");
	// Text Boxes
	TextArea txtbxLike = new TextArea();
	TextArea txtbxDisLike = new TextArea();
	HorizontalPanel headder = new HorizontalPanel();
	Label btnLogin = new Label("Login"); // Lecturer Login
	Image tum = new Image();
	Image info = new Image();
	
	private int id;
	private int opt;
	private String title;
	
	public Poll(int id, String title, EIST_POLL controller) {
		// apply style names for CSS formatting
		if (title != null) lblHeadding = new Label(title);
		else lblHeadding = new Label("Evaluation");
		lblHeadding.setStyleName("h1 h1poll");
		lblOption.setStyleName("p");
		lblLike.setStyleName("p");
		lblDislike.setStyleName("p");
		txtbxLike.setStyleName("textinput");
		txtbxDisLike.setStyleName("textinput");
		btnSend.addStyleDependentName("submit");
		
		btnLogin.addStyleName("btlogin");
		
		tum.setStyleName("tumlogo");
		info.setStyleName("infologo");
		tum.setUrl("tum.gif");
		info.setUrl("info.gif");
		
		
		// add UI Elements
		headder.add(btnLogin);
		headder.add(tum);
		headder.add(info);
		headder.setStyleName("headder");
		pollPanel.add(headder);
		pollPanel.add(lblHeadding);
		pollPanel.add(lblOption);
		pollPanel.add(rdbtnVerySatisfied);
		pollPanel.add(rdbtnSatisfied);
		pollPanel.add(rdbtnNeutral);
		pollPanel.add(rdbtnUnsatisfied);
		pollPanel.add(rdbtnVeryUnsatisfied);
		pollPanel.add(lblLike);
		pollPanel.add(txtbxLike);
		pollPanel.add(lblDislike);
		pollPanel.add(txtbxDisLike);
		pollPanel.add(btnSend);
		pollPanel.addStyleName("container");
		
		// pollPanel.add(btnLogin);
		
		// store poll id
		this.id = id;
		opt=-1;
		this.controller = controller;
		this.title = title;
		// add click Handler
		btnSend.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				submitPoll();
			}
		});
		// add click Handler
		btnLogin.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				triggerLogin();
			}
		});
	}

	// As always: Getter and Setter
	@Override
	public VerticalPanel getContainer() {
		return pollPanel;
	}
	
	private void triggerLogin(){
		controller.triggerLogin();
	}
	
	private void submitPoll(){
		getOption();
		if (opt==-1){
			this.displayErrorMessage("Error: Please select at lease one option!");
			return;
		}
		controller.submitPoll(this.id, this.opt, this.txtbxLike.getText(), this.txtbxDisLike.getText());	
	}
	
	@Override
	public void displayErrorMessage(String message) {
		// TODO Auto-generated method stub
		if (lblErrMsg == null){
			lblErrMsg = new Label(message);
			lblErrMsg.setStyleName("error");
			pollPanel.add(lblErrMsg);
		}
		else{
			lblErrMsg.setText(message);
		}
	}
	
	public int getOption(){
		if (rdbtnVerySatisfied.getValue()) opt = 0;
		if (rdbtnSatisfied.getValue()) opt = 1;
		if (rdbtnNeutral.getValue()) opt = 2;
		if (rdbtnUnsatisfied.getValue()) opt = 3;
		if (rdbtnVeryUnsatisfied.getValue()) opt = 4;
		return opt;
	}
	
	public String getLike(){
		return txtbxLike.getText();
	}
	
	public String getDisLike(){
		return txtbxDisLike.getText();
	}
	
	public int getId(){
		return id;
	}

}
